package laschools.team1.Zelda;

import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class Terrain {

	// Rock images
		protected static Image rock1;
		protected static Image rockBot;
		protected static Image rockTop;
		protected static Image rockBotLeft;
		protected static Image rockBotRight;
		protected static Image rockTopLeft;
		protected static Image rockTopRight;

		// Water images
		protected static Image waterMid;
		protected static Image waterTop;
		protected static Image waterBot;
		protected static Image waterLeft;
		protected static Image waterRight;
		protected static Image waterTopRight;
		protected static Image waterTopLeft;
		protected static Image waterBotRight;
		protected static Image waterBotLeft;

		// Tree images
		protected static Image Tree1;
		protected static Image Tree2;

		// Bush images
		protected static Image bush1;
		protected static Image bushTop;
		protected static Image bushBot;
		protected static Image bushTopRight;
		protected static Image bushBotRight;
		protected static Image bushTopLeft;
		protected static Image bushBotLeft;

		// Dirt Images
		protected static Image Dirt1;

		// Cave
		protected static Image Cave;
		
		//Stair
		protected static Image stair1;

	public Terrain() {
		waterMid = new ImageIcon("Sprites/waterMid.png").getImage();
		waterTop = new ImageIcon("Sprites/waterTop.png").getImage();
		waterBot = new ImageIcon("Sprites/waterBot.png").getImage();
		waterLeft = new ImageIcon("Sprites/waterLeft.png").getImage();
		waterRight = new ImageIcon("Sprites/waterRight.png").getImage();
		waterTopRight = new ImageIcon("Sprites/waterTopRight.png").getImage();
		waterTopLeft = new ImageIcon("Sprites/waterTopLeft.png").getImage();
		waterBotRight = new ImageIcon("Sprites/waterBotRight.png").getImage();
		waterBotLeft = new ImageIcon("Sprites/waterBotLeft.png").getImage();

		Tree1 = new ImageIcon("Sprites/Tree1.png").getImage();
		Tree2 = new ImageIcon("Sprites/Tree2.png").getImage();

		rock1 = new ImageIcon("Sprites/rock1.png").getImage();
		rockBot = new ImageIcon("Sprites/rockBot.png").getImage();
		rockTop = new ImageIcon("Sprites/rockTop.png").getImage();
		rockTopLeft = new ImageIcon("Sprites/rockTopLeft.png").getImage();
		rockTopRight = new ImageIcon("Sprites/rockTopRight.png").getImage();
		rockBotRight = new ImageIcon("Sprites/rockBotRight.png").getImage();
		rockBotLeft = new ImageIcon("Sprites/rockBotLeft.png").getImage();


		bush1 = new ImageIcon("Sprites/bush1.png").getImage();
		bushTop = new ImageIcon("Sprites/bushTop.png").getImage();
		bushBot = new ImageIcon("Sprites/bushBot.png").getImage();
		bushTopRight = new ImageIcon("Sprites/bushTopRight.png").getImage();
		bushBotRight = new ImageIcon("Sprites/bushBotRight.png").getImage();
		bushTopLeft = new ImageIcon("Sprites/bushTopLeft.png").getImage();
		bushBotLeft = new ImageIcon("Sprites/bushBotLeft.png").getImage();

		Dirt1 = new ImageIcon("Sprites/Dirt.png").getImage();
		Cave = new ImageIcon("Sprites/Cave.png").getImage();
		stair1 = new ImageIcon("Sprites/Stairs1.png").getImage();
		
	}
	
	public static void drawTerrain(Graphics g, char[][] screen){
		for (int i = 0; i < 11; i++) {
			for (int j = 0; j < 16; j++) {
				int x = Main.getSquareLength() * j;
				int y = (Main.getSquareLength() * i) + Main.getHUDheight();
				Image tempImage = rock1;
				
				switch (screen[j][i]) {
				case 't':
					tempImage = Tree1;
					break;
				case 'd':
					tempImage = Dirt1;
					break;
				case 'w':
					if (i == 0 || i == 10) {
						if (j >= 1 && j <= 14 && screen[j + 1][i] != 'w') {
							if (screen[j - 1][i] != 'w') {
								tempImage = waterMid;
							} else {
								tempImage = waterRight;
							}
						} else if (j >= 1 && screen[j - 1][i] != 'w') {
							tempImage = waterLeft;
						} else {
							tempImage = waterMid;
						}
					} else if (j == 0) {
						if (screen[0][i + 1] != 'w') {
							if (screen[0][i - 1] != 'w') {
								tempImage = waterMid;
							} else if (screen[1][i] != 'w') {
								tempImage = waterBotRight;
							} else {
								tempImage = waterBot;
							}
						} else if (screen[0][i - 1] != 'w') {
							if (screen[1][i] != 'w') {
								tempImage = waterTopRight;
							} else {
								tempImage = waterBot;
							}
						} else {
							if (screen[j + 1][i] != 'w') {
								tempImage = waterRight;
							} else {
								tempImage = waterMid;
							}
						}
					} else if (j == 15) {
						if (screen[15][i + 1] != 'w') {
							if (screen[15][i - 1] != 'w') {
								tempImage = waterMid;
							} else if (screen[14][i] != 'w') {
								tempImage = waterBotLeft;
							} else {
								tempImage = waterBot;
							}
						} else if (screen[15][i - 1] != 'w') {
							if (screen[1][i] != 'w') {
								tempImage = waterTopLeft;
							} else {
								tempImage = waterBot;
							}
						} else {
							if (screen[14][i] != 'w') {
								tempImage = waterLeft;
							} else {
								tempImage = waterMid;
							}
						}
					} else {
						if (screen[j][i - 1] != 'w' && screen[j][i - 1] != 'c') {
							if (screen[j][i + 1] != 'w') {
								tempImage = waterMid;
							} else if (screen[j - 1][i] != 'w'
									&& screen[j - 1][i] != 'c') {
								tempImage = waterTopLeft;
							} else if (screen[j + 1][i] != 'w'
									&& screen[j + 1][i] != 'c') {
								tempImage = waterTopRight;
							} else {
								tempImage = waterTop;
							}
						} else if (screen[j][i + 1] != 'w'
								&& screen[j][i + 1] != 'c') {
							if (screen[j - 1][i] != 'w'
									&& screen[j - 1][i] != 'c') {
								tempImage = waterBotLeft;
							} else if (screen[j + 1][i] != 'w'
									&& screen[j + 1][i] != 'c') {
								tempImage = waterBotRight;
							} else {
								tempImage = waterBot;
							}
						} else {
							if (screen[j + 1][i] != 'w') {
								if (screen[j - 1][i] != 'w') {
									tempImage = waterMid;
								} else {
									tempImage = waterRight;
								}
							} else if (screen[j - 1][i] != 'w') {
								tempImage = waterLeft;
							} else {
								tempImage = waterMid;
							}
						}
					}
					break;
				case 'r':
					if (i == 0 || i == 10) {
						tempImage = rockBot;
					} else if (j == 0) {
						if (screen[0][i + 1] != 'r') {
							if (screen[1][i] != 'r') {
								tempImage = rockBotRight;
							} else {
								tempImage = rockBot;
							}
						} else if (screen[0][i - 1] != 'r') {
							if (screen[1][i] != 'r') {
								tempImage = rockTopRight;
							} else {
								tempImage = rockTop;
							}
						} else {
							tempImage = rockBot;
						}
					} else if (j == 15) {
						if (screen[15][i + 1] != 'r') {
							if (screen[14][i] != 'r') {
								tempImage = rockBotLeft;
							} else {
								tempImage = rockBot;
							}
						} else if (screen[15][i - 1] != 'r') {
							if (screen[14][i] != 'r') {
								tempImage = rockTopLeft;
							} else {
								tempImage = rockTop;
							}
						} else {
							tempImage = rockBot;
						}
					} else {
						if (screen[j - 1][i] == 'r' || screen[j + 1][i] == 'r'
								|| screen[j][i - 1] == 'r'
								|| screen[j][i + 1] == 'r') {
							if (screen[j][i - 1] != 'r'
									&& screen[j][i - 1] != 'c') {
								if (screen[j - 1][i] != 'r'
										&& screen[j - 1][i] != 'c') {
									tempImage = rockTopLeft;
								} else if (screen[j + 1][i] != 'r'
										&& screen[j + 1][i] != 'c') {
									tempImage = rockTopRight;
								} else {
									tempImage = rockTop;
								}
							} else if (screen[j][i + 1] != 'r'
									&& screen[j][i + 1] != 'c') {
								if (screen[j - 1][i] != 'r'
										&& screen[j - 1][i] != 'c') {
									tempImage = rockBotLeft;
								} else if (screen[j + 1][i] != 'r'
										&& screen[j + 1][i] != 'c') {
									tempImage = rockBotRight;
								} else {
									tempImage = rockBot;
								}
							} else {
								tempImage = rockBot;
							}
						} else {
							tempImage = rock1;
						}
					}
					break;
				case 'b':
					if (i == 0 || i == 10) {
						tempImage = bushBot;
					} else if (j == 0) {
						if (screen[0][i + 1] != 'b') {
							if (screen[1][i] != 'b') {
								tempImage = bushBotRight;
							} else {
								tempImage = bushBot;
							}
						} else if (screen[0][i - 1] != 'b') {
							if (screen[1][i] != 'b') {
								tempImage = bushTopRight;
							} else {
								tempImage = bushTop;
							}
						} else {
							tempImage = bushBot;
						}
					} else if (j == 15) {
						if (screen[15][i + 1] != 'b') {
							if (screen[14][i] != 'b') {
								tempImage = bushBotLeft;
							} else {
								tempImage = bushBot;
							}
						} else if (screen[15][i - 1] != 'b') {
							if (screen[14][i] != 'b') {
								tempImage = bushTopLeft;
							} else {
								tempImage = bushTop;
							}
						} else {
							tempImage = bushBot;
						}
					} else {
						if (screen[j - 1][i] == 'b' || screen[j + 1][i] == 'b'
								|| screen[j][i - 1] == 'b'
								|| screen[j][i + 1] == 'b') {
							if (screen[j][i - 1] != 'b'
									&& screen[j][i - 1] != 'c') {
								if (screen[j - 1][i] != 'b'
										&& screen[j - 1][i] != 'c') {
									tempImage = bushTopLeft;
								} else if (screen[j + 1][i] != 'b'
										&& screen[j + 1][i] != 'c') {
									tempImage = bushTopRight;
								} else {
									tempImage = bushTop;
								}
							} else if (screen[j][i + 1] != 'b'
									&& screen[j][i + 1] != 'c') {
								if (screen[j - 1][i] != 'b'
										&& screen[j - 1][i] != 'c') {
									tempImage = bushBotLeft;
								} else if (screen[j + 1][i] != 'b'
										&& screen[j + 1][i] != 'c') {
									tempImage = bushBotRight;
								} else {
									tempImage = bushBot;
								}
							} else {
								tempImage = bushBot;
							}
						} else {
							tempImage = bush1;
						}
					}
					break;
				case 'f':
					tempImage = Tree2;
					break;
				case 's':
					tempImage = stair1;
					break;
				default:
					tempImage = Cave;
					break;
				}
				
				g.drawImage(tempImage, x, y, Main.getSquareLength(), Main.getSquareLength(), null);
				
			}
		}

	}
}
